package com.seocoo.worker.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seocoo.worker.R;

/**
 * 左面TextView，右面 ImageView
 */
public class LtvRIvLayout extends LinearLayout {

    private TextView tvLeft;
    private ImageView ivRight;
    private ImageView ivIcon;

    public LtvRIvLayout(Context context) {
        this(context,null);
    }

    public LtvRIvLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LtvRIvLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    private void initView(@NonNull AttributeSet attrs) {

        setGravity(Gravity.CENTER);
        LayoutParams left = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        left.gravity = Gravity.CENTER ;
        left.weight = 1;

        this.tvLeft = new TextView(getContext());
        this.tvLeft.setGravity(Gravity.CENTER | Gravity.LEFT);
        this.tvLeft.setCompoundDrawablePadding(15);
        this.tvLeft.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        this.addView(tvLeft, left);


        this.ivRight = new ImageView(getContext());
        this.addView(ivRight, new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        ivIcon = new ImageView(getContext());
        ivIcon.setScaleType(ImageView.ScaleType.CENTER);
        ivIcon.setPadding(7,0,0,0);
        this.addView(ivIcon,new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        TypedArray attributes = this.getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.baseAppToolBerView, 0, 0);
        if (attributes == null) {
            return;
        }

        String leftTxt = attributes.getString(R.styleable.baseAppToolBerView_leftTxt);
        float leftSize = attributes.getDimension(R.styleable.baseAppToolBerView_leftSize, 30);
        int leftColor = attributes.getColor(R.styleable.baseAppToolBerView_leftColor, Color.BLACK);
        Drawable leftIcon = attributes.getDrawable(R.styleable.baseAppToolBerView_leftIcon);
        String leftHintTxt = attributes.getString(R.styleable.baseAppToolBerView_leftHintTxt);
        int leftHintColor = attributes.getColor(R.styleable.baseAppToolBerView_leftHintColor, Color.BLACK);

        this.tvLeft.setText(leftTxt);
        this.tvLeft.setTextColor(leftColor);
        this.tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftSize);
        this.tvLeft.setCompoundDrawablesWithIntrinsicBounds(leftIcon, null, null, null);
        this.tvLeft.setHintTextColor(leftHintColor);
        this.tvLeft.setHint(leftHintTxt);

        float rightSize = attributes.getDimension(R.styleable.baseAppToolBerView_rightSize, 80);
        Drawable rightIcon = attributes.getDrawable(R.styleable.baseAppToolBerView_rightIcon);

        LayoutParams iv = (LayoutParams) ivRight.getLayoutParams();
        iv.height = (int) rightSize;
        iv.width = (int) rightSize;
        this.ivRight.setLayoutParams(iv);

        this.ivIcon.setImageDrawable(rightIcon);

        attributes.recycle();
    }

    public TextView getTvLeft() {
        return tvLeft;
    }

    public ImageView getIvIcon() {
        return ivIcon;
    }

    public ImageView getIvRight() {
        return ivRight;
    }
}
